package com.zs.leymovie.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.adapter.ActivityPtrAdapter;
import com.zs.leymovie.bean.AppBean;
import com.zs.leymovie.bean.DataEntity;
import com.zs.leymovie.bean.PromotionDataEntity;
import com.zs.leymovie.constants.UrlConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 *  * *  作者：张小冬
 * 时间：2015年8月17日20:53:28
 * 功能：活动界面
 */
public class ActivityFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2{
    @ViewInject(R.id.activity_ptrlistview)
    private PullToRefreshListView ptrListView;
    private ActivityPtrAdapter ptrAdapter;
    private List<PromotionDataEntity> lists = new ArrayList<>();
    private BitmapUtils bitmapUtils;
    private HttpUtils httpUtils;
    private HttpHandler handler;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        ViewUtils.inject(this,view);
        initView();
        initData();
        return view;
    }

    private void initData() {
         handler = httpUtils.send(HttpRequest.HttpMethod.GET,UrlConstants.ACTIVITY, new RequestCallBack<String>() {
             @Override
             public void onSuccess(ResponseInfo<String> responseInfo) {
                ptrListView.onRefreshComplete();
                 String result = responseInfo.result;
                 AppBean app = JSONObject.parseObject(result,AppBean.class);
                 if(app!=null && app.getStatus() == 1){
                     DataEntity data =  app.getData();
                     List<PromotionDataEntity> promodata =  data.getPromotion_data();
                         if(promodata !=null && promodata.size()>0){
                             if(lists!=null){
                                 lists.clear();
                             }
                             lists.addAll(promodata);
                             ptrAdapter.notifyDataSetChanged();
                         }
                 }
             }

             @Override
             public void onFailure(HttpException e, String s) {
                 ptrListView.onRefreshComplete();
                 Toast.makeText(getActivity(),"数据加载失败，请稍候再试",Toast.LENGTH_SHORT).show();
             }
         });
    }

    private void initView() {
        bitmapUtils = new BitmapUtils(getActivity());
        httpUtils = new HttpUtils();
        ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        ptrListView.setOnRefreshListener(this);
        ptrAdapter = new ActivityPtrAdapter(lists,getActivity(),bitmapUtils);
        ptrListView.setAdapter(ptrAdapter);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }
}
